﻿using System;
using System.Windows;
using System.Windows.Media;
using System.Windows.Automation.Peers;
using System.Windows.Controls.Primitives;
using System.Globalization;
using Gmantis.Controls.Automation;

namespace Gmantis.Controls.Extended
{
    [TemplateVisualState(Name = "Left", GroupName = "DirectionStates"), TemplateVisualState(Name = "Down", GroupName = "DirectionStates"), TemplateVisualState(Name = "Right", GroupName = "DirectionStates"), TemplateVisualState(Name = "Up", GroupName = "DirectionStates")]
    public class ExpanderButton : ToggleButton
    {
        // Fields
        internal bool _isLoaded;
        private AutomationPeer _peer;
        public static readonly DependencyProperty CornerRadiusProperty = DependencyProperty.Register("CornerRadius", typeof(CornerRadius), typeof(ExpanderButton), new PropertyMetadata(new CornerRadius(0.0)));
        public static readonly DependencyProperty DisabledBrushProperty = DependencyProperty.Register("DisabledBrush", typeof(Brush), typeof(ExpanderButton), null);
        public static readonly DependencyProperty ExpandDirectionProperty = DependencyProperty.Register("ExpandDirection", typeof(ExpandDirection), typeof(ExpanderButton), new PropertyMetadata(ExpandDirection.Down, new PropertyChangedCallback(OnExpandDirectionPropertyChanged)));
        public static readonly DependencyProperty FocusBrushProperty = DependencyProperty.Register("FocusBrush", typeof(Brush), typeof(ExpanderButton), null);
        public static readonly DependencyProperty MouseOverBrushProperty = DependencyProperty.Register("MouseOverBrush", typeof(Brush), typeof(ExpanderButton), null);
        public static readonly DependencyProperty PressedBrushProperty = DependencyProperty.Register("PressedBrush", typeof(Brush), typeof(ExpanderButton), null);

        // Methods
        public ExpanderButton()
        {
            RoutedEventHandler handler = null;
            base.DefaultStyleKey = typeof(ExpanderButton);
            if (handler == null)
                handler = (param0, param1) => this.ChangeVisualStateDirection(false);

            base.Loaded += handler;
        }

        protected void ChangeVisualStateDirection(bool useTransitions)
        {
            if (this.ExpandDirection == Extended.ExpandDirection.Left)
                VisualStateHelper.GoToState(this, "Left", useTransitions);
            if (this.ExpandDirection == Extended.ExpandDirection.Right)
                VisualStateHelper.GoToState(this, "Right", useTransitions);
            if (this.ExpandDirection == Extended.ExpandDirection.Up)
                VisualStateHelper.GoToState(this, "Up", useTransitions);
            if (this.ExpandDirection == Extended.ExpandDirection.Down)
                VisualStateHelper.GoToState(this, "Down", useTransitions);
        }

        public override void OnApplyTemplate()
        {
            string str = string.Empty;
            base.OnApplyTemplate();
            this._isLoaded = true;
            if (!string.IsNullOrEmpty(str))
            {
                this._isLoaded = false;
                if ((base.Template != null) && !PlatformIndependent.IsInDesignMode(this))
                    throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, "Template cannot be applied to ExpanderButton.\nDetails: {0}",
                        new object[] { str }));
            }
            else
                this.ChangeVisualStateDirection(false);
        }

        protected override AutomationPeer OnCreateAutomationPeer()
        {
            if (this._peer == null)
                this._peer = AutomationPeerFactory.CreateAutomationPeer(this, typeof(ExpanderButton));

            if (this._peer == null)
                return base.OnCreateAutomationPeer();

            return this._peer;
        }

        private static void OnExpandDirectionPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            (d as ExpanderButton).ChangeVisualStateDirection(true);
        }

        // Properties
        public CornerRadius CornerRadius
        {
            get { return (CornerRadius)base.GetValue(CornerRadiusProperty); }
            set { base.SetValue(CornerRadiusProperty, value); }
        }

        public Brush DisabledBrush
        {
            get { return (Brush)base.GetValue(DisabledBrushProperty); }
            set { base.SetValue(DisabledBrushProperty, value); }
        }

        public ExpandDirection ExpandDirection
        {
            get { return (ExpandDirection)base.GetValue(ExpandDirectionProperty); }
            set { base.SetValue(ExpandDirectionProperty, value); }
        }

        public Brush FocusBrush
        {
            get { return (Brush)base.GetValue(FocusBrushProperty); }
            set { base.SetValue(FocusBrushProperty, value); }
        }

        public Brush MouseOverBrush
        {
            get { return (Brush)base.GetValue(MouseOverBrushProperty); }
            set { base.SetValue(MouseOverBrushProperty, value); }
        }

        public Brush PressedBrush
        {
            get { return (Brush)base.GetValue(PressedBrushProperty); }
            set { base.SetValue(PressedBrushProperty, value); }
        }
    }

    public enum ExpandDirection
    {
        Down,
        Up,
        Left,
        Right
    }
}
